<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

</head>

<body>
    <h1>拦截器练习</h1>
    <input id="ipt" type="text">
    <button id="btn">提交按钮</button>
    <script>

        //创造axios实例 配置实例
        const Axios1 = axios.create({
            baseURL: '/',
            timeout: 10000,
            headers: {}
        })

        //添加请求拦截器
        Axios1.interceptors.request.use(function (config) {
            //获取token 让请求头携带token
            const token = "lkhsdalkhjflsa.asdasfadsfdasf.asfasfasdfasdf";
            //给请求头添加token属性 赋值给token
            config.headers.token = token
            //放行
            return config;
        }, function (err) {
            // 对请求错误做些什么
        }

        )

        //获取元素
        const oIpt = document.getElementById('ipt')
        const oBtn = document.getElementById('btn')
        //绑定点击事件
        oBtn.onclick = async function () {
            const iptValue = oIpt.value;
            try {
                const result = await Axios1.get('/user', {
                    params: {
                        userId: iptValue
                    }
                })
            } catch (e) {
                console.log(e, '出现错误');

            }

        }

    </script>
</body>

</html>